﻿//////////////////////
/// 保存全局变量 常量
//////////////////////
namespace Manhua2.Common
{
    using Manhua2.Provider;
    using System;
    using System.IO;
    using System.Threading.Tasks;
    using System.Xml.Linq;
    using Windows.UI;
    using Windows.UI.Xaml.Media;

    public class ClientOption
    {
        public Color Background { get; set; }

        public bool L2R { get; set; }

        public string DefaultProviderName { get; set; }
    }

    public static class Global
    {
        static Global()
        {
            Loader = new Windows.ApplicationModel.Resources.ResourceLoader();

            DefaultDialogTitle = Loader.GetString("DefaultDialogTitle");
            InformationDialogTitle = Loader.GetString("InformationDialogTitle");
        }

        public static async Task InitailizeAsync()
        {
            var folder = Windows.Storage.ApplicationData.Current.LocalFolder;
            try
            {
                var file = await folder.GetFileAsync("setting");
                if (file == null)
                    throw new Exception();

                using (var stream = await file.OpenStreamForReadAsync())
                {
                    var xml = XElement.Load(stream);
                    var color = xml.Element("background");
                    ClientOption = new ClientOption() 
                    {
                        Background = Color.FromArgb(
                                                255,
                                                byte.Parse(color.Element("r").Value),
                                                byte.Parse(color.Element("g").Value),
                                                byte.Parse(color.Element("b").Value)
                                            ),
                        DefaultProviderName = xml.Element("provider").Value,
                        L2R = bool.Parse(xml.Element("l2r").Value),
                    };
                }

                var style = (Windows.UI.Xaml.Style)App.Current.Resources.MergedDictionaries[0]["LayoutRootStyle"];
                var setter = (Windows.UI.Xaml.Setter)style.Setters[0];
                var brush = ((SolidColorBrush)setter.Value);
                brush.Color = ClientOption.Background;
            }
            catch
            {
                var style = (Windows.UI.Xaml.Style)App.Current.Resources.MergedDictionaries[0]["LayoutRootStyle"];
                var setter = (Windows.UI.Xaml.Setter)style.Setters[0];
                var brush = ((SolidColorBrush)setter.Value);
                ClientOption = new ClientOption()
                {
                    Background = brush.Color,
                    DefaultProviderName = "acg178",
                    L2R = false
                };
            }
        }

        public static async Task SaveSettingsAsync()
        {
            var folder = Windows.Storage.ApplicationData.Current.LocalFolder;
            try
            {
                var file = await folder.CreateFileAsync(
                    "setting",
                    Windows.Storage.CreationCollisionOption.OpenIfExists);
                
                var xml = XElement.Parse(
                     string.Format(
@"<setting>
    <background>
        <r>{0}</r>
        <g>{1}</g>
        <b>{2}</b>
    </background>
    <provider>{3}</provider>
    <l2r>{4}</l2r>
</setting>",
                         ClientOption.Background.R,
                         ClientOption.Background.G,
                         ClientOption.Background.B,
                         ClientOption.DefaultProviderName,
                         ClientOption.L2R));

                using (var stream = await file.OpenStreamForWriteAsync())
                {
                    xml.Save(stream);
                    stream.SetLength(stream.Position);
                }
            }
            catch
            {
            }
        }

        #region 字符串
        public static Windows.ApplicationModel.Resources.ResourceLoader Loader;

        public static string DefaultDialogTitle;

        public static string InformationDialogTitle;

        
        #endregion

        /// <summary>
        /// 数据库文件名
        /// </summary>
        public const string DataBase = "cache.db";

        public static LocalFileProvider FileProvider = null;
                
        //public static string DefaultProviderName = "bengou";
        ////public static string DefaultProviderName = "4399";

        //private static bool _l2r = false;
        //public static bool L2R
        //{
        //    get { return Global._l2r; }
        //    set { Global._l2r = value; }
        //}

        public static ClientOption ClientOption
        {
            get;
            set;
        }
    }
}
